<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>汽车详细信息</title>
    <link href="css/swiper-5.4.5/swiper.min.css" rel="stylesheet">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/font-awesome.css" rel="stylesheet">
    <link href="css/index.css" rel="stylesheet">
    <link href="css/my.css" rel="stylesheet">
    <link href="css/nav.css" rel="stylesheet">
    <style>
        [v-cloak]{
            display: none;
        }

        .ql-editor img {
            width: 600px;
        }
    </style>
</head>
<body>
<div id="wrapper">
    <!-- 头部-->
    <div style="width: 100%; height: 60px;line-height: 60px;background-color: #333333">
        <div class="container">
            <div class="row">
                <div class="col-md-8">
                    <div style="color: #b3d8ff;font-size: 20px;">电动甄选</div>
                </div>
                <div class="col-md-4" style="text-align: right;color: #EEEEEE;"v-if="user.name">
                    <span style="color: #b3d8ff;margin-right: 20px;">{{user.name}}</span>
                    <a style="color: #EEEEEE" href="javascript:void (0)"@click="logout">退出</a>

                </div>
                <div v-else class="col-md-4"style="text-align: right;">
                    <a style="color: #EEEEEE;margin-right: 10px;" href="/end/page/login.html" target="_blank">登录</a>
                    <a style="color: #EEEEEE" href="/end/page/register.html" target="_blank">注册</a>

                </div>
            </div>
        </div>
    </div>

    <!--导航-->
    <div class="container" style="margin: 5px auto">
        <div style="width: 100%;height: 50px;line-height: 50px">
            <div class="col-md-12">
                <div class="row">
                    <ul style="display: flex;">
                        <li class="nav-item"><a href="index.html">首页</a> </li>
                        <li class="nav-item"><a href="noticeInfo.html">公告</a> </li>
                        <li class="nav-item"><a href="videoInfo.html">新能源介绍</a> </li>
                        <li class="nav-item active"><a href="richTextInfo.html">汽车详细信息</a> </li>
                        <li class="nav-item"><a href="messageInfo.html">论坛</a> </li>
                        <li class="nav-item"><a href="cartInfo.html">购物车</a> </li>
                        <li class="nav-item"><a href="orderInfo.html">订单</a> </li>
                        <li class="nav-item"><a href="commentInfo.html">评价</a> </li>
                        <li class="nav-item"><a href="javascript:void(0);"@click="personalPage">个人信息</a> </li>
                        <li class="nav-item" v-if="isShow"><a href="/end/page/index.html" target="_blank">进入后台系统</a> </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <!--主题开始-->
    <div class="container" style="margin-top: 20px;">
        <div class="info">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <div class="panel panel-default">
                        <div class="panel-body bg-gray">
                            <div class="col-md-2">名称：</div>
                            <div class="col-md-2">{{obj.name}}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <div class="panel panel-default">
                        <div class="ql-editor panel-body" id="view-notice-body">

                        </div>
                    </div>
                </div>
            </div>
        <div class="comment">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <h2>评论</h2>
                </div>
            </div>
            <div class="comment-item">
                <div class="row mt-10">
                    <div class="col-md-8 col-md-offset-2" style="text-align: justify">
                        <textarea style="resize: none;width: 100%;" rows="3" v-model="comment.content"></textarea>
                    </div>
                </div>
                <div class="row mt-10">
                    <div class="col-md-8 col-md-offset-2" style="text-align: right;">
                       <button class="btn btn-primary f-r" @click="add()">发送</button>
                        <hr>
                    </div>
                </div>
            </div>
            <div class="comment-item">
                <div v-for="item in comments" class="row mt-10">
                    <div class="col-md-8 col-md-offset-2">
                        <div class="col-md-2" style="text-align: right;"><b>{{item.name}}</b></div>
                        <div class="col-md-10">
                            <div class="row">
                                <div class="col-md-8" style="text-align: justify;">{{item.content}}</div>
                                <div class="col-md-4" style="text-align: right;">{{item.createTime}}</div>
                                <div style="color: #888888" v-for="child in item.children">
                                    <div class="col-md-12" style="margin-top: 10px;">{{child.name}}回复：</div>
                                    <div class="col-md-8" style="text-align: justify;">{{child.content}}</div>
                                    <div class="col-md-4" style="text-align: right;">{{child.createTime}}</div>
                                </div>
                                <div class="col-md-6" style="margin-top: 10px;">
                                    <input type="text" class="form-control" style="width: 80%; display: inline-block;" v-model="item.replyText">
                                    <button class="btn btn-sm" @click="reply(item.id,item.replyText)">回复</button>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12"><hr style="margin: 10px 0"/> </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="js/jquery-1.10.2.js"></script>
<script src="js/swiper-5.4.5/swiper.min.js"></script>
<script src="js/vue2.6.11/axios.js"></script>
<script src="js/vue2.6.11/vue.min.js"></script>
<script src="js/my.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.metisMenu.js"></script>
<script>
    new Vue({
        el:'#wrapper',
        data:{
            user: {},
            isShow: false, //是否显示进入后台管理
            obj:{},         //汽车详细信息
            fileSrc: '',
            comment:{
                content:'',
                foreignId: 0
            },
            comments:[],  //评论列表

        },
        created: function (){
            axios.get("/auth").then(res =>{
               if(res.data.code==='0'){
                   this.user = res.data.data;
                   if (this.user.level !== 3){
                       this.isShow = true;
                   }
               }
            });
            let id = this.getQueryVariable('id');
            this.comment.foreignId =id;
            axios.get('/richtextInfo/'+id).then(res =>{
                if(res.data.code==='0'){
                    let obj = res.data.data;
                    this.obj = res.data.data;
                    if (obj.fileName){
                        if(/\.(png|jpg|gif|jpeg|webp|jfif)$/.test(obj.fileName)){
                            this.obj.fileType = 'image';
                        }else if (/\.mp4$/.test(obj.fileName)){
                            this.obj.fileType = 'video';
                        }else {
                            this.obj.fileType = 'other';
                        }
                        this.fileSrc = '/files/download'+obj.fileId;
                        let img = new Image();
                        img.src = this.fileSrc;
                        let _this = this;
                        img.onload = function (){
                            if(img.width>590){
                                _this.imageWidth = "100%";
                            }else {
                                _this.imageWidth = img.width+"px";
                            }
                        }
                    }
                    $('#view-notice-body').html(obj.content);
                }
            });
            this.loadComments(id);
        },

        methods:{
            logout(){
                axios.get("/logout").then(res =>{
                    if(res.data.code==='0'){
                        location.href ='/front/index.html'
                    }
                });
            },

            //获取传来的参数
            getQueryVariable: function (variable) {
                let query = window.location.search.substring(1);
                let vars = query.split("&");
                for (let i=0;i<vars.length;i++){
                    let pair = vars[i].split("=");
                    if (pair[0] === variable){
                        return pair[1];
                    }
                }
            },

            //发送评论
            add(){
                if(this.comment.content ===''){
                    return;
                }
                axios.post('/richtextInfoComment',this.comment).then(res =>{
                    if(res.data.code==='0'){
                        this.comment.content = '';
                        alert('评论成功！');
                        this.loadComments(this.comment.foreignId);
                    }else {
                        alert(res.data.msg);
                    }
                });

            },

            //评论列表
            loadComments(id){
                axios.get('/richtextInfoComment/findByForeignId/'+id).then(res =>{
                    if(res.data.code==='0'){
                        this.comments = res.data.data;
                    }else {
                        alert(res.data.msg);
                    }
                });
            },

            //回复
            reply(id,content){
                let user = this.user;
                if(!user||!user.name){
                    alert("请先登录");
                    return;
                }
                if (!content){
                    alert("请输入回复内容");
                    return;
                }
                let data = {content: content,name: user.name,foreignId: this.comment.foreignId,parentId:id};
                axios.post('/richtextInfoComment',data).then(res =>{
                    if(res.data.code==='0'){
                        alert('回复成功！');
                        this.loadComments(this.comment.foreignId);
                    }else {
                        alert(res.data.msg);
                    }
                });
            }


        }
    });
</script>
</body>
</html>